package org.micro.alibaba.provider.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.micro.alibaba.common.domain.Item;
import org.micro.alibaba.common.domain.Order;
import org.micro.alibaba.common.domain.User;
import org.micro.alibaba.provider.feign.UserFeignClient;
import org.micro.alibaba.provider.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author keven
 * @date 2025-07-03 10:33 上午
 * @Description
 */
@Slf4j
@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> {

    @Autowired
    private ItemService itemService;

    public void createOrder(User user, Item item, Integer num) {
        Order order = new Order();
        order.setUserId(user.getId());
        order.setOrderNo(System.currentTimeMillis() + "");
        order.setUsername(user.getUsername());
        order.setMobile(user.getMobile());
        order.setItemId(item.getId());
        order.setNum(num);

        BigDecimal price = item.getPrice();
        BigDecimal amount = price.multiply(new BigDecimal(num));
        order.setAmount(amount);
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        baseMapper.insert(order);

        itemService.updateNum(item.getId(),num);
    }

}
